document.addEventListener("DOMContentLoaded", function () {
  // 找出所有轮播容器（会议、活动、讨论）
  const carousels = document.querySelectorAll('.banner-slides, .news-slides');

  carousels.forEach((carousel) => {
    // 找轨道
    let track = carousel.querySelector('.banner-track, .news-track');
    if (!track) {
      track = carousel.firstElementChild;
    }

    const slides = track.children;
    if (slides.length <= 1) return; // 只有一张图不轮播

    let index = 0;
    const total = slides.length;

    // 布局设置
    track.style.display = 'flex';
    track.style.transition = 'transform 0.5s ease';
    Array.from(slides).forEach(slide => {
      slide.style.flex = '0 0 100%';
    });

    // 切换函数
    function goToSlide(i) {
      track.style.transform = `translateX(-${i * 100}%)`;
    }

    function nextSlide() {
      index = (index + 1) % total;
      goToSlide(index);
    }

    // 自动轮播
    let timer = setInterval(nextSlide, 3000);

    // 鼠标悬停暂停
    carousel.addEventListener('mouseenter', () => clearInterval(timer));
    carousel.addEventListener('mouseleave', () => {
      timer = setInterval(nextSlide, 3000);
    });
  });
});
